<?
include("./_common.php");
include("$g4[path]/lib/shop.lib.php");

if(!$OID || $OID == "")
{
	echo("<script>alert('잘못된 접속입니다.');self.close();</script>");
}
else
{
	$order_row = mysql_fetch_array(mysql_query("select * from g4_order where oid = '".$OID."'  limit 1"));

	$tmp_status_history = explode("/",$order_row[status_history]);

	if($status == $order_row[status])
	{
		$sql = "update g4_order set tranc='".$tranc."', tranc_num='".$tranc_num."' where oid='".$OID."' limit 1";
	}
	else
	{

		if($status == "6")//취소
		{
			if($order_row[mb_part]=="member")
			{
				if($order_row[pay_point] > 0)
				{
					$point_sql = "insert into g4_point set ";
					$point_sql.= "mb_id = '".$order_row[mb_id]."', ";
					$point_sql.= "po_datetime = now(), ";
					$point_sql.= "po_content = '주문번호 [".$order_row[oid]."]건 결제 취소', ";
					$point_sql.= "po_point = '".$order_row[pay_point]."', ";
					$point_sql.= "po_rel_table = '@shop', ";
					$point_sql.= "po_rel_id = 'system', ";
					$point_sql.= "po_rel_action = '".date("Y-m-d")."' ";

					$point_rst = mysql_query($point_sql);

					if($point_rst)
					{
						$member_sql = "update g4_member set mb_point = mb_point + ".$order_row[pay_point]." where mb_id = '".$order_row[mb_id]."' limit 1";
						$member_rst = mysql_query($member_sql);
					}
				}
			}

			$order_goods_sql = "select * from g4_order_goods where oid = '".$OID."' ";
			$order_goods_rst = mysql_query($order_goods_sql);
			while($order_goods_row = mysql_fetch_array($order_goods_rst))
			{
				$goods_update_sql = "update g4_goods set g_cnt = g_cnt + ".$order_goods_row[cnt].", g_sale = g_sale - ".$order_goods_row[cnt]." where g_code='".$order_goods_row[g_code]."' limit 1";

				$goods_update_rst = mysql_query($goods_update_sql);
			}
		}

		if($order_row[status] == "6") // 취소 -> 다른상태로 변경
		{
			if($order_row[mb_part]=="member")
			{
				if($order_row[pay_point] > 0)
				{
					$point_sql = "insert into g4_point set ";
					$point_sql.= "mb_id = '".$order_row[mb_id]."', ";
					$point_sql.= "po_datetime = now(), ";
					$point_sql.= "po_content = '주문번호 [".$order_row[oid]."]건 결제취소에서 일반상태로 변경', ";
					$point_sql.= "po_point = '-".$order_row[pay_point]."', ";
					$point_sql.= "po_rel_table = '@shop', ";
					$point_sql.= "po_rel_id = 'system', ";
					$point_sql.= "po_rel_action = '".date("Y-m-d")."' ";

					$point_rst = mysql_query($point_sql);

					if($point_rst)
					{
						$member_sql = "update g4_member set mb_point = mb_point - ".$order_row[pay_point]." where mb_id = '".$order_row[mb_id]."' limit 1";
						$member_rst = mysql_query($member_sql);
					}
				}
			}

			$order_goods_sql = "select * from g4_order_goods where oid = '".$OID."' ";
			$order_goods_rst = mysql_query($order_goods_sql);
			while($order_goods_row = mysql_fetch_array($order_goods_rst))
			{
				$goods_update_sql = "update g4_goods set g_cnt = g_cnt - ".$order_goods_row[cnt].", g_sale = g_sale + ".$order_goods_row[cnt]." where g_code='".$order_goods_row[g_code]."' limit 1";

				$goods_update_rst = mysql_query($goods_update_sql);
			}
		}

		$tmp_status_history[] = $status."|".date("Y-m-d H:i:s");
		$status_history = implode("/",$tmp_status_history);

		$sql  = "update g4_order set status='".$status."', status_history='".$status_history."', tranc='".$tranc."', tranc_num='".$tranc_num."' where oid='".$OID."' limit 1";
	}

	$rst = mysql_query($sql);

	if($rst)
	{
		echo("<script>alert('정상처리 되었습니다.');location.href='./order_view.php?OID=".$OID."'</script>");
	}
	else
	{
		echo("<script>alert('처리실패!! 관리자에게 문의하세요.');history.back(-1);<script>");
	}
}
?>